package com.wkhyapp.lm.view;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.commonsdk.debug.E;
import com.wkhyapp.lm.MainActivity;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.ShareAdpater;
import com.wkhyapp.lm.adapter.ShareGoodsAdapter;
import com.wkhyapp.lm.adapter.ShareGoodsImgApater;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.constant.Constant;
import com.wkhyapp.lm.contract.SharePresenter;
import com.wkhyapp.lm.contract.ShareView;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.FileUtils;
import com.wkhyapp.lm.utils.ImgDonwloads;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class ShareGoodsActivity extends SuperActivity<SharePresenter> implements ShareView{


    @BindView(R.id.back_iv)
    ImageView back;
    @BindView(R.id.grid_view)
    GridView gridvi;
    @BindView(R.id.desc_tv)
    TextView desc_tv;
    Long goodsId;
    @BindView(R.id.copy_tv)
    TextView copy_tv;
    private ShareAdpater shareGoodsImgApater;
    private List<String> imgs = new ArrayList<>();
    private Goods goods;
    @BindView(R.id.save_rl)
     RelativeLayout save_rl;
    @BindView(R.id.share_rl)
    RelativeLayout share_rl;
    @BindView(R.id.qx_cb)
    CheckBox qx_cb;
    @Override
    protected SharePresenter createPresenter() {
        return mPresenter = new SharePresenter(this);
    }

    @Override
    public void initUI() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);
        goodsId = getIntent().getLongExtra("0",0L);
        shareGoodsImgApater = new ShareAdpater(mContext);
        shareGoodsImgApater.setData(imgs);
        shareGoodsImgApater.initCheck(true);
        gridvi.setAdapter(shareGoodsImgApater);
        mPresenter.getData(goodsId);
    }

    @Override
    protected void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        copy_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                if(!StringUtils.isEmpty(goods.getDesc())){
                    ClipData clipData = ClipData.newPlainText(null,goods.getDesc()+"");
                    // 把数据集设置（复制）到剪贴板
                    clipboard.setPrimaryClip(clipData);
                    TToast("文案已复制");
                }else{
                    TToast("文案为空,请补充文案");
                }

            }
        });

        save_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M &&
                        checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                }else{

                    System.out.println("---"+ FileUtils.makeDirs(Constant.appFilePatch));
                    System.out.println("---"+ FileUtils.makeDirs(Constant.imagFilePatch));


                    List<String> newImgs = new ArrayList<String>();
                    for (Map.Entry<Integer,Boolean> entry : shareGoodsImgApater.getMap().entrySet()) {
                        if(entry.getValue()){
                            newImgs.add(imgs.get(entry.getKey()));
                        }
                    }

                    ImgDonwloads.donwloadImgs(mContext,newImgs);
                }


            }
        });

        share_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M &&
                        checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                }else {

                    try{
                        ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clipData = ClipData.newPlainText(null, goods.getDesc() + "");
                        // 把数据集设置（复制）到剪贴板
                        clipboard.setPrimaryClip(clipData);
                    }catch (Exception e){
                        e.printStackTrace();
                    }


                    List<String> newImgs = new ArrayList<String>();
                    for (Map.Entry<Integer, Boolean> entry : shareGoodsImgApater.getMap().entrySet()) {
                        Log.e("shar", "key = " + entry.getKey() + " and value = " + entry.getValue());
                        if (entry.getValue()) {
                            newImgs.add(imgs.get(entry.getKey()));
                        }
                    }

                    ImgDonwloads.donwloadImgs(mContext, newImgs);
                    TToast("文案已复制,图片保存至手机相册,到达微信后黏贴文字,打开相册上传图片即可");

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            TToast("正在为您打开微信");
                            Intent lan = mContext.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                            Intent intent = new Intent(Intent.ACTION_MAIN);
                            intent.addCategory(Intent.CATEGORY_LAUNCHER);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setComponent(lan.getComponent());
                            startActivity(intent);
                        }
                    }, 3000);

                }
            }
        });

        qx_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    shareGoodsImgApater.initCheck(true);
                }else{
                    shareGoodsImgApater.initCheck(false);
                }
                shareGoodsImgApater.notifyDataSetChanged();
            }
        });
    }


    @Override
    public void setGoods(Goods goods) {
        this.goods = goods;
        desc_tv.setText(goods.getDesc()+"");
        imgs.addAll(goods.getGoodsImgs());
        shareGoodsImgApater.initCheck(true);
        shareGoodsImgApater.notifyDataSetChanged();
    }



    @Override
    protected int getLayoutId() {
        return R.layout.activity_share;
    }
    @Override
    protected void afterCreate() {

    }
}
